<?php

namespace App\Http\Controllers;


use App\Notifications\FindPasswordNotify;
use App\User;
use Illuminate\Http\Request;

class PasswordController extends Controller
{
    public function email(){
        return view('password.email');
    }
    public function send(Request $request){
        $user=User::where('email',$request->input('email'))->first();
        \Notification::send($user,new FindPasswordNotify($user->email_token));
    }
    public function edit($token){
        $user=self::getEmailToken($token);
        return view('password.edit',compact('user'));
    }
    public function update(Request $request){
        $this->validate($request,[
           'password'=>'required|min:5|confirmed',
        ]);
        $user=self::getEmailToken($request->input('token'));
        $user->password=bcrypt($request->input('password'));
        $user->email_token=str_random(10);
        $user->save();
        session()->flash('success','密码重置成功');
        return redirect()->route('login');
    }
    protected  static function getEmailToken($token){
        return User::where('email_token',$token)->first();
    }
}
